#include "JsonDeserializer.h"

JsonDeserializer::JsonDeserializer(const char* path)
	:file_path_(path)
{
	std::ifstream f(file_path_,std::ios::in);

	if (!f.is_open())
	{
		std::string err_info = "Json file [" + file_path_ + "] doesn't exist!";
		throw err_info;
	}

	f.seekg(0,std::ios::end);
	int size = f.tellg();
	if(size > 0)
	{
		char*str = new char[size];
		f.read(str,size);
		str[size] = '\0';
		std::string s(str);
		delete str;
		f.close();
		JsonDeserializer(s.c_str());
		return;
	}
	f.close();
}

JsonDeserializer::JsonDeserializer(const std::string& jsonString)
{
	//Json::Value root;     // 创建JSON值对象
    Json::Reader reader;  // 创建JSON读取器
    std::string errorStr; // 用于存储解析错误信息

    // 假设jsonString是你要解析的JSON字符串
    if (!reader.parse(jsonString, document_, &errorStr))
    {
	 	std::string err_info = "Parse json string error!";
		throw err_info;
    }
 
	// if (document_.Parse(jsonString.c_str()).HasParseError())
	// {
	// 	std::string err_info = "Parse json string error!";
	// 	throw err_info;
	// }

	value_stack_.push(&document_);
}


std::string JsonDeserializer::GetParsePath()
{
	std::list<std::string>::iterator it = info_.begin();

	std::string path = *it++;

	for (; it != info_.end(); it++)
	{
		path += "/" + *it;
	}

	return path;
}

